package com.hxm.appGenealogy.config.ossConfig;

import com.aliyun.oss.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DelOssConfig {

    @Value("${huangye.aliyun.httpPrefile}")
    private String httpPrefile;

    @Value("${huangye.aliyun.bucketName}")
    private String bucketName;

    @Value("${huangye.aliyun.endpoint}")
    String endpoints;

    @Value("${huangye.aliyun.accessKeyId}")
    String accessKeyIds;


    @Value("${huangye.aliyun.accessKeySecret}")
    String accessKeySecrets;


    public void delImage(String url) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        //String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。
        String objectName = url;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoints, accessKeyIds, accessKeySecrets);

        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

